#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Two_Radar_Update_test.py
@Time    :   2023/09/23 09:16:36
@Author  :   Andy 
@Version :   1.0
@Contact :   SongLi@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

from pytest_assume.plugin import assume
import os
import sys
import logging
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.abspath(os.path.dirname(curPath) + os.path.sep + ".")
sys.path.append(rootPath)
# sys.path.append(os.path.join(rootPath,'commDriver'))
# sys.path.append(os.path.join(rootPath,'data'))
sys.path.append(os.path.join(rootPath,'download'))
# logging.info(sys.path)

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
import allure
import sys
from collections.abc import Iterable
from commDriver.modbus_plc import *
from DownLoad import *
sys.dont_write_bytecode = True
import Set_up


'''选择刷写文件'''
FlashDrivepath = Set_up.FlashDrivepath
Bootpath = Set_up.Bootpath
Apppath = Set_up.Apppath

class Test_Flash(object):       #改测试需要在Set_up.py中修改雷达类型为all_two
    def get_frame(self,Driver,Right = True):
        if Right:
            Driver['CAN'].fush_q_buff()  # 清缓存
            time.sleep(2)
            ALl_data, buffer_size = Driver['CAN'].read_frame()
            VCAN_Data = ALl_data[ALl_data['AIdentifier']==0x44C]
            time.sleep(2)
            VCAN_Data_fsp = Driver['CAN'].get_fps(0x44C,'CANFD')
        else:
            Driver['CAN'].fush_q_buff()  # 清缓存
            time.sleep(2)
            ALl_data, buffer_size = Driver['CAN'].read_frame()
            VCAN_Data = ALl_data[ALl_data['AIdentifier']==0x449]
            time.sleep(2)
            VCAN_Data_fsp = Driver['CAN'].get_fps(0x449,'CANFD')
        return VCAN_Data,VCAN_Data_fsp

    def update_LRCR(self,Driver):
        dl = Download(Driver['CAN'],Driver['UDSLRCR'],FlashDrivepath,Bootpath,Apppath,skipboot=False)
        dl.join()
        with assume: assert dl.success_flag == True
        dl.post_abnormal_termination_startup()
        with assume: assert dl.normal == True
        datas,fsp = self.get_frame(Driver,Right = False)
        logging.info('datas is empty? :{}'.format(datas.empty))
        with assume: assert 1 <= fsp
        with assume: assert datas.empty == False
        
    def update_RRCR(self,Driver):
        dl = Download(Driver['CAN'],Driver['UDS'],FlashDrivepath,Bootpath,Apppath,skipboot=False)
        dl.join()
        with assume: assert dl.success_flag == True
        dl.post_abnormal_termination_startup()
        with assume: assert dl.normal == True
        datas,fsp = self.get_frame(Driver,Right = True)
        logging.info('datas is empty? :{}'.format(datas.empty))
        with assume: assert 1 <= fsp
        with assume: assert datas.empty == False
    
    def update_cycle(self,Driver):
        dl = Download(Driver['CAN'],Driver['UDS'],FlashDrivepath,Bootpath,Apppath,skipboot=False)
        dl.join()
        with assume: assert dl.success_flag == True
        dl.post_abnormal_termination_startup()
        with assume: assert dl.normal == True

    # @pytest.mark.skip()
    def test_LRCR_RRCR(self,Driver):
        self.update_LRCR(Driver)
        self.update_RRCR(Driver)

    # @pytest.mark.skip()
    def test_RRCR_LRCR(self,Driver):      
        self.update_RRCR(Driver)
        self.update_LRCR(Driver)

    @pytest.mark.repeat(20)
    def test_cycle_Flash(self,Driver):      #循环刷写,wakeup and sleep
        logging.info('test start!')
        Driver['CAN'].wakeup_ctrl(True)
        Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PEPS_0x1B1', 'PwrModVld', 0x02)
        time.sleep(5)
        self.update_cycle(Driver)
        Driver['CAN'].wakeup_ctrl(False)
        Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PEPS_0x1B1', 'PwrModVld', 0x00)
        time.sleep(5)
        logging.info('test end!')
        
    @pytest.mark.repeat(20)
    def test_cycle_Flash_poweroff(self,Driver):      #循环刷写 poweron and poweroff
        logging.info('test start!') 
        Driver['power'].Enable(True)       
        Driver['CAN'].wakeup_ctrl(True)
        Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PEPS_0x1B1', 'PwrModVld', 0x02)
        time.sleep(5)
        self.update_cycle(Driver)
        Driver['CAN'].wakeup_ctrl(False)
        Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PEPS_0x1B1', 'PwrModVld', 0x00)
        time.sleep(0.5)
        Driver['power'].Enable(False)
        time.sleep(0.5)
        logging.info('test end!')

    # @pytest.mark.repeat(20)
    # def test_cycle_wakeup(self,Driver):      #BUSSLEEP_WAKEUP_FLASH 
    #     logging.info('test start!')
    #     Driver['CAN'].wakeup_ctrl(False)
    #     Driver['CAN'].write_db_signal(Set_up.NetName, 'CEM', 'PEPS2', 'SysPowerMod', 0x00)
    #     time.sleep(10)
    #     sleepmode = False
    #     start_time = time.time()
    #     while( (time.time()-start_time) < 5):
    #         Radar_NM_message_fsp = Driver['CAN'].get_fps(0x52A,'CANFD')  # NM CAN1
    #         v,i =Driver['power'].Read_Measure()
    #         sleepmode = True if ( i < 0.08) else False   #开机fsp返回-1
    #         sleepmode = True if ((Radar_NM_message_fsp == 0 or Radar_NM_message_fsp == -1)) else False   #开机fsp返回-1
    #         if sleepmode:
    #             break
    #         time.sleep(0.1)
    #     if sleepmode:
    #         Driver['CAN'].wakeup_ctrl(True)
    #         time.sleep(5)
    #         self.update_cycle(Driver)
    #     else:
    #         logging.info('Not sleep!')
    #     logging.info('test end!')
        